package arithmetic.sort;

/**
 * Created by jiangfeng on 2017/10/10.
 */
public class BubbleSort {
    public static void bubbleSort2(Integer[] a) {
        for (int i = 0; i < a.length; i++)
            for (int j = i + 1; j < a.length; j++)
                if (a[j] < a[i]) swap(i, j, a);
    }

    /**
     * 错误的，因为冒泡每次要比较，一次不满足则永远不执行了
     * @param a
     */
    public static void bubbleSort(Integer[] a) {
        for (int i = 0; i < a.length; i++)
            for (int j = i + 1; j < a.length && a[j] < a[i]; j++)
                swap(i, j, a);
    }

    public static void swap(int i, int j, Integer[] a) {
        a[i] = a[i] + a[j];
        a[j] = a[i] - a[j];
        a[i] = a[i] - a[j];
    }
}
